package com.zp.self.springboot.自动装配.import注解导入;

import com.zp.self.springboot.自动装配.Circle;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Field;

@Import({Circle.class})//里面没有显式声明任何的 Bean，然后将刚才创建的 Circle 导入。
@Configuration
public class MainConfig {

    //测试
    public static void main(String[] args) throws IllegalAccessException {
        //IOC容器中能查询到 Circle  bean
        ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
        Circle circle = context.getBean(Circle.class);
        MainConfig appConfig = context.getBean(MainConfig.class);
        circle.sayHi();

        Field $$beanFactoryField = ReflectionUtils.findField(appConfig.getClass(), "$$beanFactory");
        BeanFactory beanFactory = (BeanFactory) $$beanFactoryField.get(appConfig);

        System.out.println(beanFactory == context.getAutowireCapableBeanFactory());
        System.out.println(beanFactory == context);
        System.out.println(appConfig instanceof BeanFactoryAware);
        System.out.println(appConfig.getClass().getInterfaces()[0]);

    }


} 